package com.itheima.web;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import com.itheima.service.serviceImpl.UserServiceImpl;
import com.itheima.web.base.BaseServlet;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

@WebServlet("/userServlet")
public class UserServlet extends BaseServlet {
    //更改用户信息
    public void update(HttpServletRequest request, HttpServletResponse response) {
        try {
            //获取要修改的id
            Map<String, String[]> map = request.getParameterMap();

            User user = new User();

            BeanUtils.populate(user, map);

            //调用service中的方法
            UserService service = new UserServiceImpl();
            service.updateUser(user);

            //重定向到userServlet
            response.sendRedirect(request.getContextPath() + "/userServlet?method=findAll");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //通过id查找
    public void findById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取查询用户id
        String id = request.getParameter("id");

        //调用service查询
        UserService service = new UserServiceImpl();
        User user = service.findById(id);

        //将用户信息存到请求域当中
        request.setAttribute("user", user);

        //转发到update.jsp界面
        request.getRequestDispatcher("/update.jsp").forward(request, response);

    }

    //删除用户
    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取删除的id
        String id = request.getParameter("id");

        //调用service
        UserService service = new UserServiceImpl();
        service.deleteById(id);

        //重定向查询
        response.sendRedirect(request.getContextPath() + "/userServlet?method=findAll");

    }

    //添加用户
    public void add(HttpServletRequest request, HttpServletResponse response) {
        try {
            //获取表单里提交的map集合
            Map<String, String[]> map = request.getParameterMap();

            //利用beanutils封装学生对象
            User user = new User();
            BeanUtils.populate(user, map);

            //调用service
            UserService service = new UserServiceImpl();
            service.addUser(user);

            //重定向到userListServlet查询
            response.sendRedirect(request.getContextPath() + "/userServlet?method=findAll");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //查询所有
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1. 调用service查询，并返回一个用户集合
        UserService service = new UserServiceImpl();
        List<User> users = service.findAll();

        //2. 将用户集合存到请求域当中
        request.setAttribute("users", users);

        //3. 请求转发到list.jsp中显示一下
        request.getRequestDispatcher("/list.jsp").forward(request, response);
    }
}
